/*
 *  Copyright 2009 Piotr Soróbka <psorobka@gmail.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

//package your.group.id;

import javax.ejb.EJB;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author Piotr Soróbka <psorobka@gmail.com>
 */
public class HelloController {
    @EJB
    private HelloLocal helloService;

    //properties
    private String name;
    private String greeting;
    private int firstItem = 0;
    private int batchSize = 5;
    private String sortBy;

    private DataModel model;

    /**
     * default empty constructor
     */
    public HelloController() {
    }

    //-------------------getter & setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGreeting() {
        return greeting;
    }

    public int getFirstItem() {
        return firstItem;
    }

    public int getLastItem() {
        int size = getItemCount();
        return firstItem + batchSize > size ? size : firstItem + batchSize;
    }

    public int getBatchSize() {
        return batchSize;
    }

    public String getSortBy() {
        return sortBy;
    }

    //-------------------actions

    public String sendAction() {
        greeting = helloService.sayHello( this.getName() );
        return "success";
    }
	
    public DataModel getItems() {
       model = new ListDataModel( helloService.getItems( firstItem,batchSize,getSortBy() ) );
       //DataModel model = new PagedListDataModel(helloService.getItems( firstItem,batchSize),122,100);
       return model;       
    }

    public String prev() {
        firstItem -= batchSize;
        if (firstItem < 0) {
            firstItem = 0;
        }
        return "success";
    }

    public String next() {
       if (firstItem + batchSize < getItemCount()) {
           firstItem += batchSize;
       }
       return "success";
   } 

   public String end() {
	   int modular = getItemCount() % batchSize;
	   if ( modular > 0 )
	   	firstItem = getItemCount() - modular;
	   else
		firstItem = getItemCount() - batchSize;

       return "success";
   }

   public String begin() {
           firstItem = 0;

       return "success";
   }

   public int getItemCount() {
        int count = helloService.getItemCount();
        return count;
   }

   public String sortName() {
        firstItem = 0;
	sortBy = "name_";
        return "success";
   }

   public String sortDate() {
        firstItem = 0;
        sortBy = "date_";
        return "success";
   }

}
